package org.lex.swing.tab;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.lex.utils.IconCache;


public class CloseHeader extends JPanel implements HasIcon, HasTitle, HasToolTip {
	private JLabel label = null;
	private JButton button = null;

	public CloseHeader() {
		this.initialize();
	}

	private void initialize() {
		GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
		gridBagConstraints1.gridx = 1;
		gridBagConstraints1.insets = new Insets(0, 6, 0, 0);
		gridBagConstraints1.gridy = 0;
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridy = 0;
		gridBagConstraints.ipady = 0;
		gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.gridx = 0;
		this.setLayout(new GridBagLayout());
		this.label = new JLabel();
		this.label.setOpaque(false);
		// ----------------------------
		this.button = new JButton();
		button.setIcon(IconCache.getIcon(IconCache.CLOSE_ICON));
		button.setToolTipText("Close");
		button.setBorderPainted(false);
		button.setContentAreaFilled(false);
		button.setOpaque(false);
		button.setFocusPainted(false);
		button.setPreferredSize(new Dimension(18, 18));
		button.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseEntered(MouseEvent e) {
				button.setBorderPainted(true);
			}

			@Override
			public void mouseExited(MouseEvent e) {
				button.setBorderPainted(false);
			}
		});
		// ----------------------------
		this.setOpaque(false);
		this.add(label, gridBagConstraints);
		this.add(button, gridBagConstraints1);
	}

	public JButton getButton() {
		return this.button;
	}

	// icon
	public Icon getIcon() {
		return label.getIcon();
	}

	public void setIcon(Icon icon) {
		label.setIcon(icon);
	}

	// title
	public String getTitle() {
		return label.getText();
	}

	public void setTitle(String title) {
		label.setText(title);
	}

	// tooltip
	public String getToolTip() {
		return label.getToolTipText();
	}

	public void setToolTip(String tip) {
		label.setToolTipText(tip);
	}
}
